package com.thd.springboottest.security.securitycustom;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * com.thd.springboottest.security.securitycustom.MyToken
 *
 * @author: wanglei62
 * @DATE: 2022/6/22 15:23
 **/
public class ThirdToken extends AbstractAuthenticationToken {
    // 认证信息,用于存储能识别用户的内容 例如：用户名/手机/邮箱
    private final Object principal;
    // 口令,用于存放密码,短信验证码,邮件验证码等信息的
    private Object credentials;

    private String loginType ;

    // 这个构造适用于创建登录请求认证的Token (尚未认证)
    public ThirdToken(Object principal, Object credentials,String loginType) {
        super((Collection)null);
        this.principal = principal;
        this.credentials = credentials;
        this.loginType = loginType;
        this.setAuthenticated(false);
    }

    // 这个构造适用于创建登录请求认证的Token (尚未认证)
    public ThirdToken(Object principal, Object credentials) {
        super((Collection)null);
        this.principal = principal;
        this.credentials = credentials;
        this.setAuthenticated(false);
    }

    // 这个构造函数适用于已经认证成功的Token(已通过认证)
    public ThirdToken(Object principal, Object credentials,String loginType, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        this.loginType = loginType;
        super.setAuthenticated(true);
    }
    public Object getCredentials() {
        return this.credentials;
    }

    public Object getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    public void eraseCredentials() {
        super.eraseCredentials();
        this.credentials = null;
    }

    public String getLoginType() {
        return loginType;
    }

    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }
}
